
#include <string.h>
#include <stdint.h>
#include <wget.h>

int main()
{
  char *method = "POST",
//       *uri = "0.0.0.0:8888/shells",
       //*uri = "https://gitee.com/static/images/logo-black.svg",
       *uri = "gitee.com";//,
       //*post_data = "{test:\"ss\"}";
//  size_t data_len = strlen(post_data);
  int ret = false;
  void *res = NULL;
  wget_http_connection *conn = NULL;
  wget_http_request *req;
  wget_http_response *resp;
  wget_iri *iri;

  printf("[DEBUG] [L%d f-%s] method:%s url:%s\n", __LINE__, __func__, method, uri);
  /*
  wget_global_init(
     // WGET_DEBUG_STREAM, stderr, WGET_ERROR_STREAM, stderr,
      //WGET_INFO_STREAM, stdout,
      0);
      */

  iri = wget_iri_parse(uri, NULL);
  req = wget_http_create_request(iri, method);
  wget_http_add_header(req, "Connection", "keepalive");
  /*
  if (post_data) {
   // printf("[DEBUG] [L%d f-%s] body:%s\n", __LINE__, __func__, post_data);
    //wget_http_request_set_body(req, "application/x-www-form-urlencoded",
    wget_http_request_set_body(req, "application/json",
        wget_memdup(post_data, data_len), data_len);
  }
  */
  wget_http_open(&conn, iri);
  if (conn) {
    if (!wget_http_send_request(conn, req)) {
      resp = wget_http_get_response(conn);
      if (resp && (resp->body != NULL)) {
        printf("reason:%s code:%d \n", resp->reason, resp->code);
        res = calloc(1, resp->body->length+2);
        memcpy(res, resp->body->data, resp->body->length);
        wget_http_free_response(&resp);
      }
    }
  }
  wget_http_free_request(&req);
  wget_iri_free(&iri);
  wget_http_close(&conn);
//  wget_global_deinit();
  printf("[DEBUG]L%d f-%s res:%s\n", __LINE__, __func__, (char *)res);

  return ret;
}
